"""
Case Type   : datakit安全中心
Case Name   : 删除白名单地址
Create At   : 2025/2/7
Owner       : @sungang14
Description :
    1. 点击安全中心-访问白名单
    2. 选择待删除白名单记录，点击操作“删除”
    3. 点击“确认”【结果1】
Expect      :
    1、【结果1】删除成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.Api import Api


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.sys_api = Api(self.token)
        self.ip_address = '168.1.1.125'
        self.ip_address_new = '168.1.1.126'
        self.title_name = 'whitelist_test0043'
        self.whitelist_data = {"title": self.title_name, "ipList": self.ip_address}

        text = '----查询待添加白名单是否存在，存在则删除----'
        self.log.info(text)
        res1 = self.sys_api.get_white_list(self.title_name)
        self.log.info(res1.json())
        self.num_role = res1.json()['total']
        self.log.info(self.num_role)
        if self.num_role != 0:
            self.whitelist_id = res1.json()['rows'][0]['id']
            res2 = self.sys_api.delete_white_list(self.whitelist_id)
            self.log.info(res2.json())
            self.assertEqual(200, res2.json()['code'], '删除白名单失败')
            self.assertEqual("success", res2.json()['msg'], '删除白名单失败')
        text = '----点击安全中心-访问白名单，点击“添加白名单“添加有效IP地址和名称----'
        self.log.info(text)
        self.data3 = {"title": self.title_name,
                      "ipList": self.ip_address}
        res4 = self.sys_api.add_whitelist(self.data3)
        self.log.info(res4.json())
        self.assertEqual(200, res4.json()['code'], '增加白名单失败')
        self.assertEqual("success", res4.json()['msg'], '增加白名单失败')

    def test_batch_installation(self):
        text = '----step1:选择待删除白名单记录，点击操作“删除” expect:删除成功----'
        self.log.info(text)
        res1 = self.sys_api.get_white_list(self.title_name)
        self.log.info(res1.json())
        self.whitelist_id = res1.json()['rows'][0]['id']
        res2 = self.sys_api.delete_white_list(self.whitelist_id)
        self.log.info(res2.json())
        self.assertEqual(200, res2.json()['code'], '删除白名单失败')
        self.assertEqual("success", res2.json()['msg'], '删除白名单失败')

    def tearDown(self):
        self.log.info(f"无后置操作处理")
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
